// src/directives/permission.js
import { useAuthStore } from '@/stores/auth'

/**
 * 权限指令
 * v-permission="'user:create'"
 * v-permission="['user:create', 'user:update']"
 */
export default {
  mounted(el, binding) {
    const { value } = binding
    
    if (!value) return
    
    const authStore = useAuthStore()
    
    let hasPermission = false
    
    if (Array.isArray(value)) {
      // 数组形式，只要有一个权限就显示
      hasPermission = authStore.hasAnyPermission(value)
    } else {
      // 字符串形式
      hasPermission = authStore.hasPermission(value)
    }
    
    if (!hasPermission) {
      el.style.display = 'none'
      // 或者直接移除元素
      // el.parentNode && el.parentNode.removeChild(el)
    }
  },
  
  updated(el, binding) {
    // 权限变化时重新检查
    this.mounted(el, binding)
  }
}